package com.yixun.controller;

import com.yixun.entity.YwDiQu;
import com.yixun.service.YwDiQuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/ywdiqu")
public class YwDiQuController extends BaseController {

    private final YwDiQuService ywDiQuService;

    public YwDiQuController(YwDiQuService ywDiQuService) {
        this.ywDiQuService = ywDiQuService;
    }

    @RequestMapping(value = "/list")
    public String list(){
        return "org/ywdiqu/ywdiquList";
    }

    /**
     * 跳转单选业务地区页面
     */
    @RequestMapping(value = "/go_select_single_ywdiqu")
    public String goSelect_single_ywdiqu(String handle_name, String frame_name, Model model){
        model.addAttribute("handle_name", handle_name);
        model.addAttribute("frame_name", frame_name);
        model.addAttribute("dq_yw_id", this.getSessionUser().getDq_id());
        return "org/ywdiqu/select_single_ywdiqu";
    }

    /**
     * 根据业务地区id获取业务地区树
     */
    @RequestMapping(value = "/getYwDiquTree/{dq_yw_id}")
    @ResponseBody
    public String getYwDiquTree(@PathVariable(value = "dq_yw_id") Long dq_yw_id){
        return ywDiQuService.getYwDiquTree(dq_yw_id);
    }

    /**
     * 根据业务地区id获取子地区
     */
    @RequestMapping(value = "/getAllChildYwDiquTree/{dq_yw_id}")
    @ResponseBody
    public String getAllChildYwDiquTree(@PathVariable(value = "dq_yw_id") Long dq_yw_id){
        return ywDiQuService.getAllChildYwDiquTree(dq_yw_id);
    }

    /**
     * 根据业务地区id获取其子地区列表
     */
    @RequestMapping(value = "/getChildYwDiquList/{parent_id}")
    @ResponseBody
    public List<YwDiQu> getChildYwDiquList(@PathVariable(value = "parent_id") Long parent_id){
        return ywDiQuService.getChildYwDiquList(parent_id);
    }

    @RequestMapping(value = "/go_ywdiqu_add")
    public String go_ywdiqu_add(String parentID, String dq_yw_id, Model model){
        model.addAttribute("parentID", parentID);
        model.addAttribute("m_dq_yw_id", dq_yw_id);
        return "org/ywdiqu/ywdiqu_add";
    }

    /**
     * 判断业务地区id是否存在
     */
    @RequestMapping(value = "/ywdiquIDIsExist/{dq_yw_id}")
    @ResponseBody
    public boolean ywdiquIDIsExist(@PathVariable(value = "dq_yw_id") Long dq_yw_id){
        YwDiQu ywDiQu = ywDiQuService.getYwDiQuBean(dq_yw_id);
        return !ObjectUtils.isEmpty(ywDiQu);
    }

    /**
     * 添加业务地区
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YwDiQu ywDiQu){
        int row = ywDiQuService.insertYwDiQu(this.getSessionUser(), ywDiQu);
        return row == 1;
    }

    /**
     * 根据业务地区id获取业务地区
     */
    @RequestMapping(value = "/getYwDiqu/{dq_yw_id}")
    @ResponseBody
    public YwDiQu getYwDiqu(@PathVariable(value = "dq_yw_id") Long dq_yw_id){
        return ywDiQuService.getYwDiQuBean(dq_yw_id);
    }

    /**
     * 修改业务地区
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YwDiQu ywDiQu){
        int row = ywDiQuService.updateYwDiQu(this.getSessionUser(), ywDiQu);
        return row == 1;
    }

    /**
     * 删除业务地区
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(@RequestParam("id") String id){
        int row = ywDiQuService.delYwDiQu(this.getSessionUser(), id);
        return row > 0;
    }

    /**
     * 移动业务地区
     */
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @ResponseBody
    public boolean move(@RequestBody Map<String,String> map){
        int row = ywDiQuService.moveYwDiQu(this.getSessionUser(), map);
        return row > 0;
    }

    /**
     * 业务地区排序
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ResponseBody
    public boolean sort(@RequestBody Map<String, String> map){
        int row = ywDiQuService.sortYwDiQu(this.getSessionUser(), map.get("id"));
        return row > 0;
    }

}
